﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Google_Authenticator_netcore;

namespace EC.Admin.Controllers
{
    using EC.Application.Interface;
    using EC.Entity.Request.User;
    

    /// <summary>
    /// 首页
    /// </summary>
    public class HomeController : BaseController
    {
        #region 注入
        /// <summary>
        /// 权限接口
        /// </summary>
        private readonly AuthService _authService;

        /// <summary>
        /// 权限接口
        /// </summary>
        private readonly LoginService _loginService;

        /// <summary>
        /// 商户数据接口
        /// </summary>
        private readonly MerchantService _merchantService;


        /// <summary>
        /// 
        /// </summary>
        /// <param name="authService">权限接口</param>
        public HomeController(AuthService authService, MerchantService merchantService, LoginService loginService) : base(loginService)
        {
            _authService = authService;
            _loginService = loginService;
            _merchantService = merchantService;
        }
        #endregion

        /// <summary>
        /// 后台管理
        /// </summary>
        /// <returns>视图</returns>
        public IActionResult Index()
        {
            if(!string.IsNullOrEmpty(UserName))
            {
                ViewBag.Account = UserName;
            }
            //获取公司
            var merchant = _merchantService.Get(MerchantId);
            //验证
            if (merchant != null)
            {
                ViewBag.MerchantName = merchant.Name;
            }

            //ViewBag.MerchantType = merchant.Type;

            var user = _loginService.Get(UserId);
            if (user != null) {
                ViewBag.Account = user.Account;
            }
            var menus = _loginService.GetMenus(UserId);

            var depositorder = _loginService.GetDepositorder();
            if (depositorder != null)
            {
                ViewBag.depositorderCount = depositorder.Count;
                ViewBag.depositorderAmount = depositorder.Amount;
            }
            var paymentrecord = _loginService.GetPaymentrecord();
            if (paymentrecord != null)
            {
                ViewBag.paymentrecordCount = paymentrecord.Count;
                ViewBag.paymentrecordAmount = paymentrecord.Amount;
            }

            var homeStatistics = "";
            if(UserPermissions!=null && UserPermissions.Any(c=>c.Contains("Statistics")))
            {
                homeStatistics = "Statistics";
            }
            ViewBag.HomeStatistics = homeStatistics;
            return View(menus);
        }

        /// <summary>
        /// 后台管理
        /// </summary>
        /// <returns>视图</returns>
        public IActionResult Main()
        {
            TwoFactorAuthenticator tfa = new TwoFactorAuthenticator();

            //Guid.NewGuid().ToString("N").ToLower();
            //var setupInfo = tfa.GenerateSetupCode("911pay", "immi", "0000000000", 300, 300);

            //ViewBag.qrCodeImageUrl = setupInfo.QrCodeSetupImageUrl;

            //string manualEntrySetupCode = setupInfo.ManualEntryKey;

            //TwoFactorAuthenticator tfa1 = new TwoFactorAuthenticator();
            //bool isCorrectPIN = tfa1.ValidateTwoFactorPIN("0000000000", "608518", TimeSpan.FromMinutes(1));
            return View();
        }

        /// <summary>
        /// 用户[重置]
        /// </summary>
        /// <returns></returns>
        public IActionResult ResetPassWord()
        {
            var viewName = "~/Views/Home/ResetPassWord.cshtml";

            var model = _authService.GetUser(UserId);

            return View(viewName, model);
        }

        /// <summary>
        /// 修改密码
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public JsonResult ResetPassWord(ResetPassWordRequest request)
        {
            request.Id = UserId;

            var response = _authService.ResetPassWord(request);

            return Json(response);
        }
    }
}
